package gaileenchess.tests;
import gaileenchess.*;

public class BoardTest extends Test
{
    public BoardTest()
    {
        super("Board");
    }
    
    protected boolean doTests()
    {
        return test1() & test2();
    }
    
    private static boolean pieceEquals(Piece p1, Piece p2)
    {
        return ((p1 == null && p2 == null) || (p1 != null && p2 != null &&
            (p1.getType() == p2.getType() && p1.getSide() == p2.getSide())));
    }
    
    public static boolean boardEquals(Piece[][] board1, Piece[][] board2)
    {
        boolean ret = true;
        
        for(int x = 0; x < 8; x++)
        {
            for(int y = 0; y < 8; y++)
            {
                if(!pieceEquals(board1[x][y], board2[x][y]))
                {
                    ret = false;
                    break;
                }
            }
        }
        
        return ret;
    }
    
    private static Piece[][] boardConfig1()
    {
        Piece[][] pos = new Piece[8][8];
        
        /*
        
            a    b    c    d    e    f    g    h  
           ---- ---- ---- ---- ---- ---- ---- ----
        8 | BR | BN | BB | BQ | BK | BB | BN | BR | 7
           ---- ---- ---- ---- ---- ---- ---- ----
        7 | BP | BP | BP | BP | BP | BP | BP | BP | 6
           ---- ---- ---- ---- ---- ---- ---- ----
        6 |    |    |    |    |    |    |    |    | 5
           ---- ---- ---- ---- ---- ---- ---- ----
        5 |    |    |    |    |    |    |    |    | 4
           ---- ---- ---- ---- ---- ---- ---- ----
        4 |    |    |    |    |    |    |    |    | 3
           ---- ---- ---- ---- ---- ---- ---- ----
        3 |    |    |    | WP |    |    |    |    | 2
           ---- ---- ---- ---- ---- ---- ---- ----
        2 | WP | WP | WP |    | WP | WP | WP | WP | 1
           ---- ---- ---- ---- ---- ---- ---- ----
        1 | WR | WN | WB | WQ | WK | WB | WN | WR | 0
           ---- ---- ---- ---- ---- ---- ---- ----
            0    1    2    3    4    5    6    7
        
        */
        
        Board.addPiece(pos, new Rook(Common.Side.WHITE, 0, 0));
        Board.addPiece(pos, new Knight(Common.Side.WHITE, 0, 1));
        Board.addPiece(pos, new Bishop(Common.Side.WHITE, 0, 2));
        Board.addPiece(pos, new Queen(Common.Side.WHITE, 0, 3));
        Board.addPiece(pos, new King(Common.Side.WHITE, 0, 4));
        Board.addPiece(pos, new Bishop(Common.Side.WHITE, 0, 5));
        Board.addPiece(pos, new Knight(Common.Side.WHITE, 0, 6));
        Board.addPiece(pos, new Rook(Common.Side.WHITE, 0, 7));
        
        Board.addPiece(pos, new Rook(Common.Side.BLACK, 7, 0));
        Board.addPiece(pos, new Knight(Common.Side.BLACK, 7, 1));
        Board.addPiece(pos, new Bishop(Common.Side.BLACK, 7, 2));
        Board.addPiece(pos, new Queen(Common.Side.BLACK, 7, 3));
        Board.addPiece(pos, new King(Common.Side.BLACK, 7, 4));
        Board.addPiece(pos, new Bishop(Common.Side.BLACK, 7, 5));
        Board.addPiece(pos, new Knight(Common.Side.BLACK, 7, 6));
        Board.addPiece(pos, new Rook(Common.Side.BLACK, 7, 7));
        
        for(int x = 0; x < 8; x++)
        {
            if(x != 3)
            {
                pos[1][x] = new Pawn(Common.Side.WHITE, 1, x);
            }
            pos[6][x] = new Pawn(Common.Side.BLACK, 6, x);
        }
        
        Board.addPiece(pos, new Pawn(Common.Side.WHITE, 2, 3));
        
        return pos;
    }
    
    // tests basic isPathBlocked and inability to take one's own pieces, plus basic legal move
    private boolean test1()
    {
        boolean pass = true;
        
        Board board = new Board();
        board.reset();
        pass &= !board.isValidMove(0, new Board.Position(0, 2), new Board.Position(1, 3));
        pass &= board.isPathBlocked(new Board.Position(0, 2), new Board.Position(2, 4));
        pass &= !board.isValidMove(0, new Board.Position(0, 2), new Board.Position(2, 4));
        board.move(new Board.Position(1, 3), new Board.Position(2, 3), false);
        pass &= boardEquals(board.getPos(), boardConfig1());
        pass &= board.isValidMove(0, new Board.Position(0, 2), new Board.Position(1, 3));
        
        printSuccess(pass, "test1");
        
        return pass;
    }
    //TODO: test to make sure piece can't move to itself
    private boolean test2()
    {
        boolean pass = true;
        //TODO
        /*board.isValidMove(Common.Side playerSide, Position pos1, Position pos2);
        board.move(Position pos1, Position pos2);
        board.isOccupied(Position p);
        board.isOccupied(Position p, Common.Side side);
        board.isPathBlocked(Position pos1, Position pos2);
        board.isGameOver();*/
        printSuccess(pass, "test2");
        
        return pass;
    }
    
    //TODO: test3 - check and stuff
}
